﻿using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Dev.Common;
using System.Web.Http;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class JlqjtzController : BaseController
    {
        //
        // GET: /Dev/Jlqjtz/

        public ActionResult Index()
        {
            var model = new
            {
                idField = "Jlqjtz_Yqbh", //主键
                urls = DevHelper.GetIndexUrls("Jlqjtz"),
                resx = DevHelper.GetIndexResx("计量器具台账"),
                dataSource = new
                {
                },
                form = new
                {
                    Jlqjtz_Yqbh = "",
                    Jlqjtz_Yqmc = "",
                    Jlqjtz_Ggxh = "",
                    Jlqjtz_Gmsj = "",
                    Jlqjtz_Zzcj = "",
                    CreatePerson = ""
                },
                defaultRow = new
                {
                },
                setting = new
                {

                }
            };
            return View(model);
        }

        /// <summary>
        /// 计量器具台账编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
         [MvcMenuFilter(false)]
        public ActionResult Edit(string id = "")
        {
            var userName = DevHelper.GetUserName();
            var currentProject = DevHelper.GetCurrentProject();
            var data = new JlqjtzApiController().GetEditMaster(id);
            var codeService = new sys_codeService();

            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                urls = DevHelper.GetEditUrls("Jlqjtz"),
                resx = DevHelper.GetEditResx("计量器具台账"),
                dataSource = new
                {
                },
                defaultForm = new b_Jlqjtz().Extend(new
                {
                    Jlqjtz_Yqbh = id
                }),
                defaultRow = new
                {
                },
                setting = new
                {
                    postFormKeys = new string[] { "Jlqjtz_Yqbh" },
                    postListFields = new string[] { }
                }
            };
            return View(model);
        }
    }

    public class JlqjtzApiController : ApiController
    {
        public string GetNewBillNo()
        {
            return new b_JlqjtzService().GetNewCode("Jlqjtz_Yqbh", "", "", 8);
        }

        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Jlqjtz_Yqbh DESC'>
            <select>*</select>
                <from>b_Jlqjtz</from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Jlqjtz_Yqbh' cp='like'></field>
                        <field name='Jlqjtz_Gmsj' cp='daterange'></field>
                    </where>
            </settings>");
            var service = new Jw.Models.b_JlqjtzService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }

        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Jlqjtz_Yqbh' cp='equal'></field></where>
            </settings>", typeof(b_Jlqjtz).Name);
            var result = new b_JlqjtzService().Edit(formWrapper, null, data);
        }
        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new DevService().Audit(typeof(b_Jlqjtz).Name, "Jlqjtz_Yqbh", id, status, comment);
            DevHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Jlqjtz_Yqbh={0}]，请重试或联系管理员！", id);
        }
        /// <summary>
        /// 删除主表（删除主表前是删除明细)
        /// </summary>
        /// <param name="id"></param>
        public void Delete(string id)
        {
            var result = new b_JlqjtzService().Delete(ParamDelete.Instance().AndWhere("Jlqjtz_Yqbh", id));
            DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Jlqjtz_Yqbh={0}]，请重试或联系管理员！", id);
        }

        public dynamic GetEditMaster(string id)
        {
            b_Jlqjtz obj = new b_JlqjtzService().GetModel(ParamQuery.Instance().AndWhere("Jlqjtz_Yqbh", id));
            return new
            {
                form = obj,
                scrollKeys = new b_JlqjtzService().ScrollKeys("Jlqjtz_Yqbh", id.ToString())
            };
        }
    }
}